/*---------------------------Tag interaction--------------------------------------*/
function setTags(label, button){
	$(button).hide();
	//look for this tags and set the new type
	feedInterfaz = $("#txt_tags").html();
	feedInterfaz+="<div class='btn_tags' onclick=\"deleteTags('"+label+"', this);\"> "+label+"</div>";
	
	$("#txt_tags").html(feedInterfaz);
	
	feeTags = $("#txt_feedTags").get(0).value;
	feeArray = feeTags.split(";");
	feedResult ="";
	for(i=0; i< feeArray.length;i++){
		if(feeArray[i]!=""){
			if(feeArray[i].split("@@")[0]==label){
				feedResult += feeArray[i].split("@@")[0]+"@@"+feeArray[i].split("@@")[1]+"@@SELECTED;";
			}else{
				feedResult += feeArray[i]+";";
			}
		}
	}
	$("#txt_feedTags").get(0).value = feedResult;
}

function deleteTags(label, button){
	$(button).hide();
	//look for this tags and set the new type
	feedInterfaz = $("#sec_suggetions").html();
	feedInterfaz+="<div class='btn_tags' onclick=\"setTags('"+label+"', this);\"> "+label+"</div>";
	
	$("#sec_suggetions").html(feedInterfaz);
	
	feeTags = $("#txt_feedTags").get(0).value;
	feeArray = feeTags.split(";");
	feedResult ="";
	for(i=0; i< feeArray.length;i++){
		if(feeArray[i]!=""){
			if(feeArray[i].split("@@")[0]==label){
				feedResult += feeArray[i].split("@@")[0]+"@@"+feeArray[i].split("@@")[1]+"@@PROVIDER;";
			}else{
				feedResult += feeArray[i]+";";
			}
		}
	}
	$("#txt_feedTags").get(0).value = feedResult;
	
}

function newTag (){
	var label = $("#txt_ownTag").get(0).value.toLowerCase();
	feeTags = $("#txt_feedTags").get(0).value;
	feeTags += label+"@@0@@SELECTED;";
	$("#txt_feedTags").get(0).value = feeTags;
	
	feedInterfaz = $("#txt_tags").html();
	feedInterfaz+="<div class='btn_tags' onclick=\"deleteTags('"+label+"', this);\"> "+label+"</div>";
	
	$("#txt_tags").html(feedInterfaz);
	
	$("#txt_ownTag").get(0).value = "";
}
/*---------------------------Save Suggestions--------------------------------------*/
function saveDocument(){
	var enviaDatos = "title="+$("#txt_title").get(0).value;
	enviaDatos+="&description="+$("#txt_description").get(0).value;
	enviaDatos+="&uri="+$("#txt_uri").get(0).value;
	enviaDatos+="&tags="+$("#txt_feedTags").get(0).value;
	enviaDatos+="&user="+$("#txt_feedUser").get(0).value;
	enviaDatos+="&activity=test";
  $.ajax({
	  url: "services/feedback/saveSuggestion?"+enviaDatos,
	  success: saveDocumentOK,
	  start:saveDocumentStart,
	  dataType: "xml",
	  contentType: "application/xml"
	});
}

function saveDocumentOK(data){
	var docUri = $(data).find("docUri").text();
	text="";
	if(docUri!=""){
		text+="<article>";
		text +="<p>Successful transaction</p>"
		text +="<p><b>URI:</b> "+docUri+"</p>"
		text +="</article>";
		
	
	$("#txt_title").get(0).value ="";
	$("#txt_description").get(0).value ="";
	$("#txt_uri").get(0).value ="";
	$("#txt_feedTags").get(0).value ="";
	$("#txt_tags").get(0).value ="";
	$("#sec_suggetions").html("");
	$("#txt_tags").html("");
	}else{
		text+="<article>";
		text +="<p>Error transaction</p>"
		text +="</article>";
	}
	$("#sec_saveSuggetions").html(text);
}

function saveDocumentStart(){
	//para el cargando
}
/*---------------------------Get Suggestions--------------------------------------*/
function getSuggetions(){
  $.ajax({
	  url: "services/acota/suggest",
	  //url: "tool/suggest.xml", //Uri de prueba
	  data: { 
		  title: $("#txt_title").get(0).value,
		  description: $("#txt_description").get(0).value,
		  uri: $("#txt_uri").get(0).value,
		  },
	  success: SuggetionsOK,
	  start:SuggetionsStart,
	  dataType: "xml",
	  contentType: "application/xml"
	});
}

function SuggetionsOK(data){
	feedTags="";
	text="";
	var cont =0;
	$(data).find("entry").each(
		function(){
			if(cont<=10){
				var label = $(this).find('key').text();
				var value = $(this).find('value').text();
				text+="<div class='btn_tags' onclick=\"setTags('"+label+"', this);\">"+label+"</div>";
				feedTags+=label+"@@"+value+"@@PROVIDED;";
			}
			cont++;
		}
	);
	$("#txt_feedTags").get(0).value =feedTags;
	$("#sec_suggetions").html(text);
	$("#txt_label").get(0).value ="";
}

function SuggetionsStart(){
	$("#sec_suggetions").html("Cargando...");
}
/*---------------------------Documents by tag--------------------------------------*/
function getDocByTag(){
  $.ajax({
	  url: "services/feedback/DocumentByTag",
	  data: { tag: $("#txt_label").get(0).value },
	  success: docByTagOK,
	  start:docByTagStart,
	  dataType: "xml",
	  contentType: "application/xml"
	});
}

function docByTagOK(data){
	text="";
	text+="<p>Documents for tag \"<b>"+$("#txt_label").get(0).value+"</b>\"</p>";
	
	$(data).find("feedbackresources").each(
		function(){
		var title = $(this).find('title').text();
		var description = $(this).find('description').text();
		var source = $(this).find('source').text();
		text+="<article>";
		text +="<p><b>"+title+"</b></p>";
		text +="<p>"+description+"</p>"
		text +="<p><b>Uri:</b>"+source+"</p>"
		text +="</article>";
		}
	);
	
	$("#sec_resultQueries").html(text);
	$("#txt_label").get(0).value =""; 
	$("#sec_results").fadeIn('slow');
}

function docByTagStart(){
	//Función para hacer efectos de cargado de los datos
}

/*---------------------------Tags by user--------------------------------------*/
function getTagByUser(){
  $.ajax({
	  url: "services/feedback/tagByUser",
	  data: { user: $("#txt_user").get(0).value },
	  success: tagByUserOK,
	  start:tagByUserStart,
	  dataType: "xml",
	  contentType: "application/xml"
	});
}

function tagByUserOK(data){
	text="";
	text+="<p>Tag for user: \"<b>"+$("#txt_user").get(0).value+"</b>\"</p>";
	
	$(data).find("feedbacktags").each(
		function(){
		var label = $(this).find('label').text();
		var weight = $(this).find('weight').text();
		var tagType = $(this).find('tagType').text();
		text+="<article>";
		text +="<p><b>"+label+"</b> -> Peso:"+weight+";Tipo:"+tagType+"</p>"
		text +="</article>";
		}
	);
	
	$("#sec_resultQueries").html(text);
	$("#txt_user").get(0).value =""; 
	$("#sec_results").fadeIn('slow');
}

function tagByUserStart(){
	//Función para hacer efectos de cargado de los datos
}

/*---------------------------Tags by document--------------------------------------*/
function getTagByDocument(){
  $.ajax({
	  url: "services/feedback/tagByDocument",
	  data: { document: $("#txt_docuri").get(0).value },
	  success: tagByDocumentOK,
	  start:tagByDocumentStart,
	  dataType: "xml",
	  contentType: "application/xml"
	});
}

function tagByDocumentOK(data){
	text="";
	text+="<p>Tags for document: \"<b>"+$("#txt_docuri").get(0).value+"</b>\"</p>";
	
	$(data).find("feedbacktags").each(
		function(){
		var label = $(this).find('label').text();
		var weight = $(this).find('weight').text();
		var tagType = $(this).find('tagType').text();
		text+="<article>";
		text +="<p><b>"+label+"</b> -> Peso:"+weight+";Tipo:"+tagType+"</p>"
		text +="</article>";
		}
	);
	
	$("#sec_resultQueries").html(text);
	$("#txt_docuri").get(0).value ="";
	$("#sec_results").fadeIn('slow');
}

function tagByDocumentStart(){
	//Función para hacer efectos de cargado de los datos
}
